package com.szholly.data.coderule.table;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 规则表
 */
@Component("DATA_RULE")
public class RuleTable extends InterceptorTableBase implements IBaseTable {

	public final static String TABLE_NAME = "DATA_RULE";

	/**
	 * 获取单例对象
	 */
	public static RuleTable getSingleRef() {
		return (RuleTable) SpringBeanFactory.getBean("DATA_RULE");
	}
	
	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}
	
	@Override
	public String getTableGroup() {
		return "编码规则";
	}
	
	@Override
	public String getMetadataTableAliasName() {
		return "规则主表";
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("全局ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("数据版本");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_COMPANY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("企业名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_SYSTEMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("系统名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(RuleRow.FIELD_ID);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(RuleRow.FIELD_RULEID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(RuleRow.FIELD_RULENAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

		}
		return fields;
	}

	private static Map<String, RuleRow> convertTables;

	public static void InitFiles(RuleTable convertInfoTable) {
		if (convertTables == null) {
			convertTables = new HashMap<String, RuleRow>();
			List<RowBase> rows = (List<RowBase>) getSingleRef()
					.getEntityList(RowBase.class, new QueryMap());

			for (RowBase row : rows) {
				String key = ((RuleRow) row).getRULEID();
				if (!convertTables.containsKey(key))
					convertTables.put(key, (RuleRow) row);
			}
		}
	}

	public RuleRow GetRowByRuleID(String id) {
		if (convertTables == null)
			InitFiles(this);

		if (convertTables.containsKey(id))
			return convertTables.get(id);
		else
			return null;
	}
}
